home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / GETPASS.C < prev    next >
C/C++ Source or Header  |  1992-03-29  |  2KB  |  77 lines

  1. /* "@(#)$Header: getpass.c,v 1.0 92/02/10 12:07:30 ericb Rel $" */
  2.  
  3. /*
  4.   (c) Copyright 1992 Eric Backus
  5.  
  6.   This software may be used freely so long as this copyright notice is
  7.   left intact.  There is no warrantee on this software.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>    /* For exit() */
  12. #include <pc.h>        /* For getkey() */
  13.  
  14. /* This is superior to the standard getpass(), because it doesn't
  15.    overwrite previous calls and allows for passwords of any length. */
  16. int
  17. getlongpass(const char *prompt, char *password, int max_length)
  18. {
  19.     char    *p = password;
  20.     int        c, count = 0;
  21.  
  22.     /* If we can't prompt, abort */
  23.     if (fputs(prompt, stderr) < 0)
  24.     {
  25.     *p = '\0';
  26.     return -1;
  27.     }
  28.  
  29.     while (1)
  30.     {
  31.     /* Get a character with no echo */
  32.     c = getkey();
  33.  
  34.     /* Exit on interrupt (^c or ^break) */
  35.     if (c == '\003' || c == 0x100) exit(1);
  36.  
  37.     /* Terminate on end of line or file (^j, ^m, ^d, ^z) */
  38.     if (c == '\r' || c == '\n' || c == '\004' || c == '\032')
  39.         break;
  40.  
  41.     /* Back up on backspace */
  42.     if (c == '\b')
  43.     {
  44.         if (count) count--;
  45.         else if (p > password) p--;
  46.         continue;
  47.     }
  48.  
  49.     /* Ignore DOS extended characters */
  50.     if ((c & 0xff) != c) continue;
  51.  
  52.     /* Add to password if it isn't full */
  53.     if (p < password + max_length - 1)
  54.         *p++ = c;
  55.     else
  56.         count++;
  57.     }
  58.     *p = '\0';
  59.  
  60.     (void) fputc('\n', stderr);
  61.  
  62.     return 0;
  63. }
  64.  
  65. /* UNIX compatible getpass().  Returns a pointer to a null-terminated
  66.    static buffer of at most eight characters. */
  67. char *
  68. getpass(const char *prompt)
  69. {
  70.     static char        password_buffer[9];
  71.  
  72.     if (getlongpass(prompt, password_buffer, 9) < 0)
  73.     return (char *) NULL;
  74.     return password_buffer;
  75. }
  76.  
  77.